package cn.poco.pepsi.util.xmlpared;

import java.io.InputStream;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;

import android.util.Xml;
import cn.poco.pepsi.bean.Bolg;


public class BolgXmlparse {
	private final static String TAG = "BolgXmlparse";
	private static ArrayList<Bolg> bolgs = null;
	private static  Bolg bolgBean = null;
	static int  total;
	
	public static ArrayList<Bolg> getXml(InputStream inSteam) throws Exception{
		XmlPullParser xmpparse = Xml.newPullParser();   
		xmpparse.setInput(inSteam, "UTF-8");           
		int code = xmpparse.getEventType();            
		while(code!=XmlPullParser.END_DOCUMENT){
			switch (code) {
			case XmlPullParser.START_DOCUMENT:
				bolgs = new ArrayList<Bolg>();
				break;
			case XmlPullParser.START_TAG:
				if ("total".equals(xmpparse.getName())) {
					String key = xmpparse.nextText();
					total=Integer.parseInt(key);
				}
				if("blog".equals(xmpparse.getName())){
					bolgBean = new Bolg();
				}
				if(bolgBean!=null){
					if("blog-id".equals(xmpparse.getName())){
						String key = xmpparse.nextText();
						bolgBean.setId(Integer.parseInt(key));
						bolgBean.setTotal(total);
					}
					if("title".equals(xmpparse.getName())){
						String name = xmpparse.nextText();
						bolgBean.setTitle(name);
					}
					if("date".equals(xmpparse.getName())){
						String name = xmpparse.nextText();
						bolgBean.setDate(name);
					}
					if("comment".equals(xmpparse.getName())){
						String name = xmpparse.nextText();
						bolgBean.setComment(Integer.parseInt(name));
					}
					if("hit".equals(xmpparse.getName())){
						String name = xmpparse.nextText();
						bolgBean.setHit(Integer.parseInt(name));
					}
					if("type".equals(xmpparse.getName())){
						String name = xmpparse.nextText();
						bolgBean.setType(name);
					}
					if("image-url".equals(xmpparse.getName())){
						String name = xmpparse.nextText();
						bolgBean.setImgUrl(name);
					}
				}
				break;
			case XmlPullParser.END_TAG:
				if("blog".equals(xmpparse.getName())){
					bolgs.add(bolgBean);
					bolgBean= null;
				}
				break;
			}	
			code = xmpparse.next();
		}
		inSteam.close();
		return bolgs;
	}
}
